﻿using FileToVideo.Class;
using FileToVideo.Enums;
using FileToVideo.Helpers;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;

namespace FileToVideo.AddFunc
{
    /// <summary>
    /// 首帧数据格式扩展方法
    /// </summary>
    public static class FirstFrameAdd
    {
        /// <summary>
        /// 最小化字典输出
        /// </summary>
        /// <param name="frame"></param>
        /// <returns></returns>
        public static Dictionary<string, string> ToMiniDict(this FirstFrame frame)
        {
            var dict = frame.ToDict();
            dict.Remove("data"); // 移除数据内容
            return dict;
        }

        /// <summary>
        /// 简化方法
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static int GetByteCount(string input) => Encoding.UTF8.GetByteCount(input);

        /// <summary>
        /// 使用 Newtonsoft.Json 从 string 中读取数据
        /// </summary>
        /// <param name="firstFrame"></param>
        /// <param name="input"></param>
        /// <returns></returns>
        public static FirstFrame FromString(this FirstFrame firstFrame, string input)
        {
            var settings = new JsonSerializerSettings
            {
                TypeNameHandling = TypeNameHandling.All // 保留类型信息
            };

            firstFrame = JsonConvert.DeserializeObject<FirstFrame>(input, settings);
            return firstFrame;
        }

        /// <summary>
        /// 使用 Newtonsoft.Json 从 string 中读取数据
        /// </summary>
        /// <param name="firstFrameMore"></param>
        /// <param name="input"></param>
        /// <returns></returns>
        public static FirstFrameMore FromString(this FirstFrameMore firstFrameMore, string input)
        {
            var settings = new JsonSerializerSettings
            {
                TypeNameHandling = TypeNameHandling.All // 保留类型信息
            };

            firstFrameMore = JsonConvert.DeserializeObject<FirstFrameMore>(input, settings);
            return firstFrameMore;
        }
    }
}
